<?php
/**
 * 账单数据管理
 * PHP version 5
 * @package   Antonia
 * @author    Antonia <471699715@qq.com>
 * @copyright 2016 Antonia.
 */
namespace Admin\Controller;
use Think;
class BillsController extends AdmincommonController {
    /**
     * 显示快递公司列表
     * @param HttpRequest $request
     * @return html
     * @author Antonia
     */
    public function index() {
        $filters = array();
        $Bills = D("Bills");
        $_GET['p'] = $_POST['p'];
        if(!empty($_POST['createtime'])) {
            $createtime  = strtotime($_POST['createtime']);
            $createtimes = strtotime($_POST['createtimes']);
            if($createtime > $createtimes){
                 $filters['createTime']  = array('between',array($createtimes,$createtime));
            } else {
                $filters['createTime']  = array('between',array($createtime,$createtimes));
            }
        }
        $count = $Bills->where($filters)->count();
        if (!empty($_POST['numPerPage'])){
            $numPerPage = $_POST['numPerPage'];
        } else {
            $numPerPage = 20;
        }
        $Page  =  new \Think\Page($count,$numPerPage);
        $list = $Bills->limit($Page->firstRow.','.$Page->listRows)
                      ->relation(true)
                      ->where($filters)
                      ->select();
        $this->assign('list',$list);
        $this->assign('filters', $_POST);
        $this->assign ('totalCount', $count);
        $this->assign ('numPerPage', $Page->listRows);
        $this->assign ('currentPage', !empty($_GET['p'])?$_GET['p']:1);
        $this->display();
    }
    
    /**
     * 删除账单记录
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function delete() {
        $re = array();
        if (!empty($_GET['id'])) {
            $Bills = D("Bills");
            $result = $Bills->delete(remove_xss($_GET['id']));
            if (false !== $result) {
                $re['statusCode'] = "200";
                $re['message'] = "删除成功!";
            } else {
                $re['statusCode'] = "300";
                $re['message'] = "删除失败，请重试!";
            }
        } else {
                $re['statusCode'] = "300";
                $re['message'] = "该记录不存在！";
        }
        echo json_encode($re);
    }

    /**
     * 删除选定内容
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function delAll() {
        $Bills = D("Bills");
        $re = array();
        $map['id']  = array('in', explode(',', $_POST["ids"]));
        $result  = $Bills->where($map)->delete();
        if($result) {
            $re['statusCode'] = "200";
            $re['message'] = "删除成功!";
        } else {
            $re['statusCode'] = "300";
            $re['message'] = "删除失败，请重试!";
        }
        echo json_encode($re);
    }
    /**
     * 查看账单
     * @param HttpRequest $request
     * @return void
     * @author Antonia
     */
    public function bills_info() {
        $id = intval($_GET['id']);
        $re = array();
        if (empty($id)) {
            $re['statusCode'] = "300";
            $re['message'] = "该订单不存在";
            echo json_encode($re);
            exit;
        }
        $filters["billId"] = $_GET["id"];
        $info = D("Bill_subs")->where($filters)->select();
        // dump($info);
        if ($info) {
            $this->assign("info",$info);
            $this->display("info");
        } else {
            $re['statusCode'] = "300";
            $re['message'] = "该订单不存在！";
            echo json_encode($re);
        }
    }

}
?>